package com.adee.java;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * AtomicIntegerFieldUpdater 用于对已存在的非Atomic类型变量进行原子操作
 * 被更新的变量必须是volatile的，不能是final的
 */
public class Test037_AtomicIntegerFieldUpdater {
    public static void main(String[] args) {
        Student stu = new Student();
        Student.U.getAndIncrement(stu);
        stu.print();
    }

    static class Student {
        private volatile int num = 1;

        static AtomicIntegerFieldUpdater<Student> U
                = AtomicIntegerFieldUpdater.newUpdater(Student.class, "num");

        public void print() {
            System.out.println("num: " + num);
        }
    }
}
